package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 分页查询套餐
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //设置分页参数
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        //查找数据
        Page<Setmeal> page = setmealMapper.quirePage(setmealPageQueryDTO);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    /**
     * 添加套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO) {
        //克隆对象
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.addSetmeal(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
        }
        setmealDishMapper.addSetmealDish(setmealDishes);
    }

    /**
     * 起售或停售套餐
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();

        //获取套餐中的菜品
        List<SetmealDish> setmealDishes = setmealDishMapper.getDishes(id);

        for (SetmealDish setmealDish : setmealDishes) {
            //判断套餐内的所有菜品是否都起售,只要有一个菜品没有起售,这个套餐就不能起售
            Dish dish = dishMapper.getStatus(setmealDish.getDishId());
            if (dish!=null){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        setmealMapper.update(setmeal);
    }

    /**
     * 根据id查询套餐
     * @return
     */
    @Override
    public SetmealVO getSetmealById(Long id) {
        //和获取套餐信息
        Setmeal setmeal = setmealMapper.getSetmealById(id);
        //将其拷贝到Setmeal对象中
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);

        //获取套餐对应的菜品
        List<SetmealDish> setmealDish = setmealDishMapper. getSetmealDishById(setmeal.getId());
        //将其拷贝到Setmeal对象中
        setmealVO.setSetmealDishes(setmealDish);

        return setmealVO;
    }

    /**
     * 更新套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //拷贝对象
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //更新套餐
        setmealMapper.update(setmeal);

        //删除套餐对应的菜品
        setmealDishMapper.delete(setmeal.getId());

        //获取setmealDishes
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        //设置套餐id
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
        }
        //重新添加菜品
        setmealDishMapper.addSetmealDish(setmealDishes);


    }

    /**
     * 删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void deleteSetmealById(Long[] ids) {
        //判断套餐是否起售中，起售中则不删除
        int count = setmealDishMapper.getStatus(ids);
        if (count!=0){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        //删除分类
        setmealMapper.delete(ids);
        //删除分类中的菜品
        setmealDishMapper.deleteByids(ids);
    }

    /**
     * 根据分类id查询套餐
     * @return
     */
    @Override
    public List<Setmeal> getAllSetmeal(Long id) {
        List<Setmeal> setmeals = setmealMapper.getAllSetmeal(id);
        return setmeals;
    }
}
